package com.li.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.constant.statusConstant;
import com.li.mapper.ClueMapper;
import com.li.pojo.dto.CluesPageDto;
import com.li.pojo.po.*;
import com.li.pojo.vo.AssignmentVo;
import com.li.pojo.vo.falseClueVo;
import com.li.service.BusinessService;
import com.li.service.ClueService;
import com.li.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@Slf4j
public class ClueServiceImpl implements ClueService {
    @Autowired
    ClueMapper clueMapper;
    @Autowired
    private BusinessService businessService;

    @Autowired
    private UserService userService;

    @Override
    public PageResult list(CluesPageDto cluesPageDto) {
        Page<Clue> page = PageHelper.startPage(cluesPageDto.getPageNum(), cluesPageDto.getPageSize());
        clueMapper.page(cluesPageDto.getPageNum(),cluesPageDto.getPageSize(),cluesPageDto.getId(),cluesPageDto.getPhone(),cluesPageDto.getOwner(),cluesPageDto.getParams().get("beginCreateTime"),cluesPageDto.getParams().get("endCreateTime"),cluesPageDto.getStatus(),cluesPageDto.getChannel());
        PageResult result = new PageResult();
        Object params = result.getParams();
        return new PageResult(200,"查询成功",params,page.getResult(),page.getTotal());
    }

    @Override
    public void updateClue(falseClueVo falseClueVo, Integer id) {
        Clue clue =clueMapper.getByClueId(id);
        clue.setFalseCount(clue.getFalseCount()+1);
        clue.setStatus(statusConstant.CONSTANT);
        clueMapper.update(id,clue.getStatus());

    }

    @Override
    public PageResult poolPage(CluesPageDto cluesPageDto) {
        Page<Clue> page = PageHelper.startPage(cluesPageDto.getPageNum(), cluesPageDto.getPageSize());
        clueMapper.poolPage(cluesPageDto.getPageNum(),cluesPageDto.getPageSize(),cluesPageDto.getId(),cluesPageDto.getPhone(),cluesPageDto.getOwner(),cluesPageDto.getParams().get("beginCreateTime"),cluesPageDto.getParams().get("endCreateTime"),cluesPageDto.getStatus(),cluesPageDto.getChannel());
        PageResult result = new PageResult();
        Object params = result.getParams();
        return new PageResult(200,"查询成功",params,page.getResult(),page.getTotal());

    }

    @Override
    public void reasonGain(AssignmentVo assignmentVo) {
        for (Integer id : assignmentVo.getIds()) {
            clueMapper.reasonGain(id,statusConstant.PENDING_PROCESSING);
        }
    }

    @Override
    public void changeBusiness(Clue clue, Integer id) {
        clueMapper.update(id,statusConstant.TOBUSINESS);
        Clue clueId = clueMapper.getByClueId(id);
        Opportunities opportunities = new Opportunities();
        BeanUtils.copyProperties(clueId,opportunities);
        log.info("转商机：{}",opportunities);
        businessService.listSave(opportunities);
    }

    @Override
    public void updateassignment(AssignmentVo assignmentVo) {
        User user = userService.getById(assignmentVo.getUserId());
        Clue clue = new Clue();
        for (Integer id : assignmentVo.getIds()) {
            clue.setAssignId(Long.valueOf(user.getUserId()));
            clue.setOwner(user.getUserName());
            clue.setClueId(Long.valueOf(id));
            clueMapper.updateassignment(clue);
        }
    }


    @Override
    public void addClue(Clue clue) {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化日期时间对象为字符串
        String formattedDateTime = now.format(formatter);
        clue.setCreateTime(formattedDateTime);
        clue.setFalseCount(0);
        clueMapper.addClue(clue);
    }

    @Override
    public Clue find(Integer id) {
        Clue clue=clueMapper.find(id);
        return clue;
    }

    @Override
    public void modifyClu(Clue clue) {
        clueMapper.modifyClu(clue);
    }


}
